package com.example.webgobang.config;

import com.example.webgobang.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static com.example.webgobang.common.Constants.SESSION_USER_KEY;

@Slf4j
@Configuration
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(SESSION_USER_KEY) == null) {
            response.setStatus(401); //401：身份未认证
            return false;
        }
        return true;
    }
}
